local Widget = require "widgets/widget"
local Text = require "widgets/text"
local TextEdit = require "widgets/textedit"
local Image = require "widgets/image"


LabelTextBox = Class(Widget,function(self,labeltext, fieldtext, height, spacing, horiz_offset, params_label, params_field)
    Widget._ctor(self, "labeltextbox")

    local textbox_font_ratio = 0.8
    local offset = horiz_offset or 0
    local total_width = params_label.width + params_field.width + spacing
    
    -- 标签
    self.label = self:AddChild(Text(params_label.font or NEWFONT_OUTLINE, params_label.font_size or 25))
    self.label:SetString(labeltext)
    self.label:SetHAlign(ANCHOR_RIGHT)
    self.label:SetRegionSize(params_label.width, height)
    self.label:SetPosition((-total_width/2) + (params_label.width/2) + offset, 0)
    self.label:SetColour(unpack(params_label.color or {0, 0, 0, 1}))  -- 新增：支持自定义字体颜色
    
    -- 自定义背景图片
    local bgatlas = params_field.atlas or "images/global_redux.xml"
    local bgtex_normal = params_field.normal or "textbox3_gold_normal.tex"
    local bgtex_hover = params_field.hover or "textbox3_gold_hover.tex"
    local bgtex_focused = params_field.focus or "textbox3_gold_focus.tex"
    self.textbox_bg = self:AddChild(Image(bgatlas, bgtex_normal))
    self.textbox_bg:SetPosition((total_width/2) - (params_field.width/2) + offset, 0)
    self.textbox_bg:ScaleToSize(params_field.width, height)

    -- 文本框
    self.textbox = self:AddChild(TextEdit(params_field.font or NEWFONT_OUTLINE, (params_field.font_size or 25) * textbox_font_ratio, fieldtext, {0, 0, 0, 1}))
    self.textbox:SetForceEdit(true)
    self.textbox:SetPosition((total_width/2) - (params_field.width/2) + offset, 0)
    self.textbox:SetRegionSize(params_field.width - 30, height)
    self.textbox:SetHAlign(ANCHOR_LEFT)
    self.textbox:SetFocusedImage(self.textbox_bg, bgatlas, bgtex_normal, bgtex_hover, bgtex_focused)
    self.textbox:SetColour(unpack(params_field.color or {0, 0, 0, 1}))  -- 新增：支持自定义字体颜色
    
    -- 焦点逻辑
    self.OnGainFocus = function(self)
        Widget.OnGainFocus(self)
        self.textbox:OnGainFocus()
    end
    self.OnLoseFocus = function(self)
        Widget.OnLoseFocus(self)
        self.textbox:OnLoseFocus()
    end
    
    -- 帮助提示
    -- self.GetHelpText = function(self)
    --     local controller_id = TheInput:GetControllerID()
    --     local t = {}
    --     if not self.textbox.editing and not self.textbox.focus then
    --         table.insert(t, TheInput:GetLocalizedControl(controller_id, CONTROL_ACCEPT, false, false) .. " " .. STRINGS.UI.HELP.CHANGE_TEXT)
    --     end
    --     return table.concat(t, "  ")
    -- end
end)
return LabelTextBox